package cn.tedu.csmall.stock.webapi.controller;

import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.stock.service.IStockService;
import cn.tedu.csmall.stock.webapi.util.BlockErrorHandler;
import cn.tedu.csmall.stock.webapi.util.FallbackErrorHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/stock")
@Api(tags = "库存管理")
public class StockController {

    @Autowired
    private IStockService stockService;
// http://localhost:20003/base/stock/reduce/count
    @PostMapping("/reduce/count")
    @ApiOperation("减少商品库存数")
    // @SentinelResource注解标记的控制器方法，会被Sentinel管理，
    // * 在这个方法第一次运行后，可以在Sentinel仪表台界面中设置限流规则
    // "减少库存的方法"设置当前方法在仪表台中显示的名称
    @SentinelResource(value = "减少库存的方法"
                    ,blockHandlerClass = BlockErrorHandler.class
                    ,blockHandler = "blockError"
                    ,fallbackClass = FallbackErrorHandler.class
                    ,fallback = "fallbackError")
    public JsonResult reduceCommodityCount(@RequestBody StockReduceCountDTO stockReduceCountDTO){
        // 调用业务逻辑层
//        if (true){
//            throw new RuntimeException();
//        }
        stockService.reduceCommodityCount(stockReduceCountDTO);
        return JsonResult.ok("商品库存减少完成!");
    }
// 自定义限流和降级的类
}
